package com.android.lovely.ui.adapter

import android.content.Intent
import android.icu.text.DecimalFormat
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.android.lovely.data.HomeListData
import com.android.lovely.databinding.ItemHomeTagBinding
import com.android.lovely.databinding.ItemListBinding
import com.android.lovely.ext.loadWithPlaceholder
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.framework.BaseLoadAdapter
import com.android.lovely.manager.LogManager
import com.android.lovely.ui.activity.DetailActivity
import com.android.lovely.ui.activity.SearchActivity
import com.android.lovely.ui.viewmodel.HomeListViewModel
import com.kotlin.extensions.appContext
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/10 17:14
 */
class HomeListAdapter() :
    BaseLoadAdapter<HomeListData, HomeListAdapter.HomeListHolder>(
        object :
            DiffUtil.ItemCallback<HomeListData>() {
            override fun areItemsTheSame(oldItem: HomeListData, newItem: HomeListData): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: HomeListData, newItem: HomeListData): Boolean {
                return oldItem == newItem
            }

        }
    ) {
    override fun onBindViewHolder(p0: HomeListHolder, p1: Int) {
        val item = getItem(p1) ?: return
        p0.binding.apply {
            ivImage.loadWithPlaceholder(item.coverImage["url"]) {
                crossfade(true)
            }

            tvAuth.isVisible = !item.userInfo["name"].isNullOrEmpty()
            tvAuth.text = "@${item.userInfo["name"]}"

            tvName.text = item.name
            imgCount.text = item.cardNum.toString()
            collectCount.text = if (item.collectNum < 1000) {
                item.collectNum.toString()
            } else if (item.collectNum < 1000 * 1000) {
                "${DecimalFormat("0.##").format((item.collectNum.toFloat() / 1000))}K"
            } else {
                "${DecimalFormat("0.##").format((item.collectNum.toFloat() / 1000_000))}M"
            }
            ivNsfw.isVisible = item.type == 1
            tvContent.text = item.personality
            rvTag.isVisible = item.roleTag.isNotEmpty()
            if(item.roleTag.isNotEmpty()) {
                val adapter = HomeTagsAdapter()
                rvTag.layoutManager =  LinearLayoutManager(rvTag.context, RecyclerView.HORIZONTAL, false)
                rvTag.adapter =adapter
                adapter.updateList(item.roleTag.mapNotNull { it["tag_name"] })
            }
            rvTag.setOnTouchListener { _, _ ->
                true
            }
            root.clickable {
                LogManager.reportEvent(item.id, "click")
                DetailActivity.launch(item.id)
            }
        }
    }


    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): HomeListHolder {
        return HomeListHolder(p0)
    }

    class HomeListHolder(
        parent: ViewGroup,
        val binding: ItemListBinding = ItemListBinding.inflate(
            parent.layoutInflater,
            parent,
            false
        )
    ) : RecyclerView.ViewHolder(binding.root)
}


class HomeTagsAdapter() : BaseAdapter<String,ItemHomeTagBinding>() {

    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: String?,
        binding: ItemHomeTagBinding,
        position: Int
    ) {
        "#${item ?: ""}".also { binding.tag.text = it }
        binding.root.clickable {
            val intent = Intent(appContext, SearchActivity::class.java)
            intent.putExtras(bundleOf("Result" to item))
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            binding.root.context.startActivity(intent)
        }
    }

}